package com.feicheng.controller.shop.wx.fuwu.client;


import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.entity.ClientDeposit;
import com.feicheng.persistence.entity.DepositRecord;
import com.feicheng.persistence.entity.PaymentRecord;
import com.feicheng.persistence.entity.Store;
import com.feicheng.persistence.entity.StoreClient;
import com.feicheng.persistence.service.IClientDepositService;
import com.feicheng.persistence.service.IClientService;
import com.feicheng.service.IFClientService;
import com.feicheng.service.IFPayService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/wx/s/client/deposit-record")
@RequestMapping("/wx/s/client/deposit-record")
@Slf4j
public class DepositRecordController {

    @Autowired
    IFClientService fClientService;

    @Resource(name="pay-service")
    Map<String, IFPayService> payServiceMap;

    @GetMapping("/{depositId}/info")
    public ResultInfo getDeposit(@PathVariable("depositId") Integer depositId) {
        return fClientService.getDepositRecord(depositId);
    }

    @PostMapping("/{depositId}/info")
    public ResultInfo depositExPay(
            Integer clientId,
            Integer storeId,
            String openId,
            @PathVariable("depositId") Integer depositId
            ) {
        return payServiceMap.get(Constant.PAY_TYPE.WX).depositExPay(clientId, storeId, depositId, openId);
    }

    @PostMapping("/info")
    @Transactional
    public ResultInfo depositPay(Integer clientId, Integer storeId, Integer amount, String openId) {
        return payServiceMap.get(Constant.PAY_TYPE.WX).depositNewPay(clientId, storeId, amount, openId);
    }

    @PostMapping("/notice-url/{storeId}/{depositId}")
    public JSONObject depositNotice(
    		@RequestHeader("Wechatpay-Nonce") String nonce,
    		@RequestHeader("Wechatpay-Signature") String signature,
    		@RequestHeader("Wechatpay-Signature-Type") String signatureType,
    		@RequestHeader("Wechatpay-Timestamp") String timestamp,
    		@RequestHeader("Wechatpay-Serial") String serial,
    		@RequestBody String body, 
    		@PathVariable(name = "storeId") Integer storeId,
    		@PathVariable(name = "depositId") Integer depositId) {

        payServiceMap.get(Constant.PAY_TYPE.WX)
        		.depositNotice(nonce, signature, signatureType, timestamp, serial, body, storeId, depositId);

        JSONObject result = new JSONObject();
        result.put("code", "SUCCESS");
        result.put("message", "成功");
        return result;
    }

}